//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    current: 0,
    mycurrent: '审核通过',
    fileList: [],
    hao: '',
    list: [],
    myList: [],
    rowsTotal: '',
    myRowsTotal: '',
    page: 1,
    myPage: 1,
    imgApi: app.globalData.imgApi,
    user: wx.getStorageSync('user'),
    Ddate: '请选择打版日期',
    Sdate: '请选择上架日期',
    doc: ['请选择跟单人'],
    docIndex: 0,
    isMB: false,
    brand: [],
    size: [],
    colour: [],
    brandList: [],
    brandListIndx: 0
  },
  onShow() {
    this.setData({
      user: wx.getStorageSync('user'),
      page: 1,
      myList: []
    })
    if (this.data.current == 0) {
      this.data.page = 1
      this.setData({
        list: []
      })
      this.getList(this.data.page)
    } else if (this.data.current == 2) {
      this.data.myPage = 1
      this.setData({
        myList: []
      })
      this.getMyList(this.data.myPage)
    }
    this.getSettingList()
  },
  onLoad() {
    this.setData({
      user: wx.getStorageSync('user'),
      page: 1,
      myList: []
    })
    this.getDoc()
  },
  tabbarChange(e) {
    this.setData({
      current: e.detail.key,
    })
    if (e.detail.key == 2) {
      this.data.myPage = 1
      this.setData({
        myList: []
      })
      this.getMyList(this.data.myPage)
    } else if (e.detail.key == 0) {
      this.data.page = 1
      this.setData({
        list: []
      })
      this.getList(this.data.page)
    }
  },
  search: function(e) {
    var that = this
    wx.request({
      url: app.globalData.api + 'designDiagram/list',
      data: {
        page: 1,
        rows: 99,
        keyword: e.detail.value.key
      },
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function(res) {
        if (res.data.code == 200) {
          that.data.list = []
          for (var i in res.data.data.designDiagramList) {
            that.data.list.push(res.data.data.designDiagramList[i])
            that.data.list[i].img = that.data.list[i].designImage.split(',')[0]
          }
          that.setData({
            list: that.data.list
          })
        } else {
          that.setData({
            list: []
          })
        }
      }
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onChange(e) {
    var that = this
    const {
      file
    } = e.detail
    console.log(file)
    if (file.status == 'uploading') {
      wx.uploadFile({
        url: app.globalData.api + 'upload',
        filePath: file.url,
        name: 'file',
        success(res) {
          console.log(JSON.parse(res.data))
          that.data.fileList.push({
            uid: that.data.fileList.length,
            url: app.globalData.imgApi + JSON.parse(res.data).data
          })
          that.setData({
            fileList: that.data.fileList
          })
        }
      })
    }

  },

  onComplete(e) {
    console.log('onComplete', e)
    wx.hideLoading()
  },


  onRemove(e) {
    const {
      file,
      fileList
    } = e.detail
    console.log(e.detail)
    // wx.showModal({
    //   content: '确定删除？',
    //   success: (res) => {
    //     if (res.confirm) {
    this.setData({
      fileList: fileList.filter((n) => n.uid !== file.uid),
    })
    //     }
    //   },
    // })
  },
  MyonChange(e) {
    console.log('onChange', e)
    this.setData({
      mycurrent: e.detail.key,
      myPage: 1,
      myList: [],
      myListImg: ''
    })
    this.getMyList(this.data.myPage)
  },
  Bhao: function(e) {
    this.setData({
      hao: e.detail.value
    })
  },
  bindDDateChange(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      Ddate: e.detail.value
    })
  },
  bindSDateChange(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      Sdate: e.detail.value
    })
  },
  bindDocChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      docIndex: e.detail.value
    })
  },
  upload: function(e) {
    var that = this,
      imgUrl = [];
    if (e.detail.value.styleNumber == '' || e.detail.value.brand == '' || e.detail.value.designAssistant == '' || e.detail.value.paperSampleMaster == '' || e.detail.value.styleName == '' || e.detail.value.tracker == '') {
      wx.showToast({
        title: '请填写完整信息',
      })
      return
    }
    for (var i = 0; i < this.data.fileList.length; i++) {
      imgUrl.push(this.data.fileList[i].url.substr(app.globalData.imgApi.length))
    }
    wx.request({
      url: app.globalData.api + 'designDiagram/add',
      data: {
        designImage: imgUrl.join(','),
        styleNumber: e.detail.value.styleNumber,
        styleName: e.detail.value.styleName,
        designAssistant: e.detail.value.designAssistant,
        tracker: e.detail.value.tracker,
        paperSampleMaster: e.detail.value.paperSampleMaster,
        madeTemplateTime: this.data.Ddate + ' 23:59:59',
        trackerId: this.data.doc[this.data.docIndex].trackerId,
        brand: this.data.brand.join(','),
        color: this.data.colour.join(','),
        size: this.data.size.join(','),
        onLineTime: this.data.Sdate + ' 23:59:59',
      },
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.msg,
          })
          that.setData({
            empty: '',
            fileList: [],
            Sdate: '请选择打版日期',
            Ddate: '请选择上架日期',
            brand: [],
            size: [],
            colour: [],
            current: 2,
            mycurrent:'未审核',
            myPage:1
          })
          that.getMyList(that.data.myPage)

        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.errorMsg,
          })
        }
      }
    })
  },
  getList: function(page) {
    var that = this
    wx.request({
      url: app.globalData.api + 'designDiagram/list',
      data: {
        page: page,
        rows: 20,
      },
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          for (var i in res.data.data.designDiagramList) {
            that.data.list.push(res.data.data.designDiagramList[i])
            that.data.list[i].img = that.data.list[i].designImage.split(',')[0]
          }
          // console.log(that.data.list)
          that.setData({
            list: that.data.list,
            rowsTotal: res.data.data.rowsTotal
          })
          that.data.page += 1
        } else {
          that.setData({
            list: [],
          })
        }
      }
    })
  },
  onReachBottom: function() {
    if (this.data.current == 0) {
      if (this.data.rowsTotal > this.data.list.length) {
        this.getList(this.data.page)
      }
    } else if (this.data.current == 2) {
      if (this.data.myRowsTotal > this.data.myList.length) {
        this.getMyList(this.data.myPage)
      }
    }
  },
  getMyList: function(myPage) {
    var that = this
    wx.request({
      url: app.globalData.api + 'designDiagram/myList',
      data: {
        page: myPage,
        rows: 20,
        "status": this.data.mycurrent,
      },
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function(res) {
        console.log(res)
        var myListImg = [],
          myList = []
        if (res.data.code == 200) {
          for (var i in res.data.data.designDiagramList) {
            myList.push(res.data.data.designDiagramList[i])
            myListImg.push(res.data.data.designDiagramList[i].designImage.split(',')[0])
          }
          that.setData({
            myList: myList,
            myRowsTotal: res.data.data.rowsTotal,
            myListImg: myListImg
          })
          that.data.myPage += 1
        } else {
          that.setData({
            myList: [],
            myListImg: ''
          })
        }
      }
    })
  },
  goDet: function(e) {
    wx.navigateTo({
      url: '/pages/details/details?id=' + e.currentTarget.dataset.id
    })
  },
  //删除
  dele: function(e) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定删除?',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.api + 'designDiagram/' + e.target.dataset.id,
            data: {},
            method: 'DELETE',
            header: {
              Authorization: wx.getStorageSync('token')
            },
            success: (res) => {
              console.log(res)
              if (res.data.code == 200) {
                wx.showToast({
                  title: res.data.msg,
                })
                that.data.myPage = 1
                that.getMyList(1)
              } else {
                wx.showToast({
                  icon: none,
                  title: res.data.msg
                })
              }
            }
          })
        }
      }
    })

  },
  //编辑
  goEdit: function(e) {
    wx.navigateTo({
      url: '/pages/desigEdit/desigEdit?id=' + e.currentTarget.dataset.id
    })
  },

  //获取跟单人
  getDoc() {
    var that = this
    app.req('designDiagram/getPurchasingManager', '', 'GET', res => {
      console.log(res)
      if (res.data.code == 200) {
        that.setData({
          doc: res.data.data
        })
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.errorMsg,
        })
      }
    })
  },
  deMB() {
    this.setData({
      isMB: false
    })
  },
  add(e) {
    console.log(e)
    this.setData({
      isMB: true,
      name: e.currentTarget.dataset.name
    })
  },
  z_Submit(e) {
    switch (e.detail.target.dataset.name) {
      case '品牌':
        this.data.brand.push(e.detail.value.add)
        break
      case '尺码':
        this.data.size.push(e.detail.value.add)
        break
      case '颜色':
        this.data.colour.push(e.detail.value.add)
        break
    }
    this.setData({
      brand: this.data.brand,
      size: this.data.size,
      colour: this.data.colour,
      isMB: false
    })
  },
  z_dele(e) {
    console.log(e)
    switch (e.currentTarget.dataset.name) {
      case '品牌':
        this.data.brand.splice(e.currentTarget.dataset.index, 1)
        break
      case '尺码':
        this.data.size.splice(e.currentTarget.dataset.index, 1)
        break
      case '颜色':
        this.data.colour.splice(e.currentTarget.dataset.index, 1)
        break
    }
    this.setData({
      brand: this.data.brand,
      size: this.data.size,
      colour: this.data.colour
    })
  },
  getSettingList() {
    var that = this
    wx.request({
      url: app.globalData.api + '/system/getSystemSetting',
      data: '',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success(res) {
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            brandList: JSON.parse(res.data.data.brandList)
          })
        }
      }
    })
  },
  bindPickerChange: function(e) {
    this.data.brand.push(this.data.brandList[e.detail.value])
    this.setData({
      // index: e.detail.value,
      brand: this.data.brand
    })
  },
})